import { defineStore } from 'pinia'

export const useUser = defineStore('user', {
  state: () => ({
    userInfo: null
  }),

  getters: {
    getUserInfo: (state) => state.userInfo
  },

  actions: {
    setUserInfo(info) {
      this.userInfo = info
    },

    async loadUserInfo() {
      try {
        // 从sessionStorage获取用户信息
        const schoolNumber = sessionStorage.getItem('schoolNumber')
        if (schoolNumber) {
          this.userInfo = {
            schoolNumber: schoolNumber
          }
          return true
        }
        return false
      } catch (error) {
        console.error('加载用户信息失败:', error)
        return false
      }
    },

    clearUserInfo() {
      this.userInfo = null
      sessionStorage.removeItem('schoolNumber')
    }
  }
}) 